{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import mxnet as mx\n",
    "from mxnet import gluon, nd, autograd\n",
    "from mxnet.gluon import nn\n",
    "import numpy as np\n",
    "import pickle as p\n",
    "import matplotlib.pyplot as plt\n",
    "from time import time\n",
    "%matplotlib inline\n",
    "ctx = mx.gpu()\n",
    "data_dir = '/home/sinyer/python/data/cifar10'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_cifar(route = data_dir+'/cifar-10-batches-py'):\n",
    "    def load_batch(filename):\n",
    "        with open(filename, 'rb')as f:\n",
    "            data_dict = p.load(f, encoding='latin1')\n",
    "            X = data_dict['data']\n",
    "            Y = data_dict['labels']\n",
    "            X = X.reshape(10000, 3, 32,32).astype(\"float\")\n",
    "            Y = np.array(Y)\n",
    "            return X, Y\n",
    "    def load_labels(filename):\n",
    "        with open(filename, 'rb') as f:\n",
    "            label_names = p.load(f, encoding='latin1')\n",
    "            names = label_names['label_names']\n",
    "            return names\n",
    "    label_names = load_labels(route + \"/batches.meta\")\n",
    "    x1, y1 = load_batch(route + \"/data_batch_1\")\n",
    "    x2, y2 = load_batch(route + \"/data_batch_2\")\n",
    "    x3, y3 = load_batch(route + \"/data_batch_3\")\n",
    "    x4, y4 = load_batch(route + \"/data_batch_4\")\n",
    "    x5, y5 = load_batch(route + \"/data_batch_5\")\n",
    "    test_pic, test_label = load_batch(route + \"/test_batch\")\n",
    "    train_pic = np.concatenate((x1, x2, x3, x4, x5))\n",
    "    train_label = np.concatenate((y1, y2, y3, y4, y5))\n",
    "    return train_pic, train_label, test_pic, test_label\n",
    "\n",
    "def accuracy(output, label):\n",
    "    return nd.mean(output.argmax(axis=1)==label).asscalar()\n",
    "\n",
    "def evaluate_accuracy(test_data, net, ctx):\n",
    "    acc = 0.\n",
    "    for data, label in test_data:\n",
    "        data = data.as_in_context(ctx)\n",
    "        label = label.as_in_context(ctx)\n",
    "        output = net(data)\n",
    "        acc = acc + accuracy(output, label)\n",
    "    return acc / len(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_pic, train_label, test_pic, test_label = load_cifar()\n",
    "\n",
    "batch_size = 128\n",
    "train_data = gluon.data.DataLoader(gluon.data.ArrayDataset(\n",
    "    train_pic.astype('float32')/255, train_label.astype('float32')), batch_size, shuffle=True)\n",
    "test_data = gluon.data.DataLoader(gluon.data.ArrayDataset(\n",
    "    test_pic.astype('float32')/255, test_label.astype('float32')), batch_size, shuffle=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "net = gluon.nn.Sequential()\n",
    "with net.name_scope():\n",
    "    net.add(nn.Flatten())\n",
    "    net.add(nn.Dense(128, activation=\"relu\"))\n",
    "    net.add(nn.Dense(128, activation=\"relu\"))\n",
    "    net.add(nn.Dense(10))\n",
    "net.initialize(ctx=ctx)\n",
    "loss = gluon.loss.SoftmaxCrossEntropyLoss()\n",
    "trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 1e-2})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 loss:2.2190 tracc:0.1754 teacc:0.2310 time:1.713\n",
      "10 loss:1.6876 tracc:0.3997 teacc:0.4034 time:1.059\n",
      "20 loss:1.5340 tracc:0.4553 teacc:0.4553 time:1.107\n",
      "30 loss:1.4402 tracc:0.4895 teacc:0.4759 time:1.032\n",
      "40 loss:1.3723 tracc:0.5137 teacc:0.4954 time:1.030\n",
      "50 loss:1.3207 tracc:0.5341 teacc:0.5058 time:1.132\n",
      "60 loss:1.2666 tracc:0.5519 teacc:0.5116 time:1.059\n",
      "70 loss:1.2206 tracc:0.5686 teacc:0.5217 time:1.127\n",
      "80 loss:1.1794 tracc:0.5821 teacc:0.5232 time:1.247\n",
      "90 loss:1.1446 tracc:0.5956 teacc:0.5198 time:1.226\n",
      "100 loss:1.0552 tracc:0.6298 teacc:0.5390 time:1.155\n",
      "110 loss:1.0441 tracc:0.6335 teacc:0.5413 time:1.207\n",
      "tracc:0.634707 teacc:0.539557\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3Xt81NWd//HXZzK5kxuQEEjCVa4i\niiBadRVvLd572wrWrW1t2fZXt+12t61ur2u3j9622+1FbXmo21ZbtdWqaLVq1dLaigXkIldBUBII\nJpD7PTNzfn+cQUJIyACByXx5Px+PPMh35uQ755svj/ecfL7ne8acc4iISLCEkt0BEREZfAp3EZEA\nUriLiASQwl1EJIAU7iIiAaRwFxEJoAHD3czuMbMaM1vfz/NmZj8ys21mts7Mzhz8boqIyJFIZOT+\nc2DBYZ6/HJgc/1oM3Hns3RIRkWMxYLg75/4M1B2mybXAL523HCg0s9GD1UERETly4UHYRxlQ2WO7\nKv5Yde+GZrYYP7onNzd3zrRp0wbh5UVETh6rVq3a65wrHqjdYIS79fFYn2saOOeWAEsA5s6d61au\nXDkILy8icvIwszcTaTcYs2WqgIoe2+XA7kHYr4iIHKXBCPelwIfis2bOARqdc4eUZERE5MQZsCxj\nZvcD84GRZlYFfA1IB3DO/RR4ErgC2Aa0AR85Xp0VEZHEDBjuzrlFAzzvgE8NWo9EROSY6Q5VEZEA\nUriLiASQwl1EJIAU7iIiAaRwFxEJIIW7iEgAKdxFRAJI4S4iEkAKdxGRAFK4i4gEkMJdRCSAFO4i\nIg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LzvPTU37lIyUww\nsxuBq4AP9ribf1CPJdXCPZGlEIaseE36bmCTc+5/ejzVc/mGG4HHTnTfjpRz7lbnXLlzbjz+PDzv\nnPsg8AJ+CQpInWPZA1Sa2dT4Q5cAG0nB84Ivx5xjZjnx/2/7jyXlzksv/Z2LpcCH4rNmzgEa95dv\nhiozWwB8EbjGOdfW46mlwELzH340AX+R+O9H/ULOuZT6Aq7AX2F+HfhSsvtzhH0/H/9n1jpgTfzr\nCnyt+jlga/zf4cnu6xEe13zgifj3E+P/IbcBvwUyk92/BI/hDGBl/Nw8ChSl6nkB/hPYDKwH7gUy\nU+m8APfjrxd040ezN/V3LvCljNvjefAqfpZQ0o9hgGPZhq+t78+An/Zo/6X4sWwBLj+W19byAyIi\nAZRqZRkREUmAwl1EJIAU7iIiAaRwFxEJIIW7iEgAKdxFRAJI4S4iEkD/Hy8gVY7BhS8TAAAAAElF\nTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7feb8f420e80>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "epochs = 120\n",
    "\n",
    "a, b = [], []\n",
    "for epoch in range(epochs):\n",
    "    if epoch  == 100:\n",
    "        trainer.set_learning_rate(1e-3)\n",
    "    train_loss = 0.\n",
    "    train_acc = 0.\n",
    "    start = time()\n",
    "    for data, label in train_data:\n",
    "        data = data.as_in_context(ctx)\n",
    "        label = label.as_in_context(ctx)\n",
    "        with autograd.record():\n",
    "            output = net(data)\n",
    "            l = loss(output, label)\n",
    "        l.backward()\n",
    "        trainer.step(batch_size)\n",
    "        train_loss = train_loss + nd.mean(l).asscalar()\n",
    "        train_acc = train_acc + accuracy(output, label)\n",
    "    test_acc = evaluate_accuracy(test_data, net, ctx)\n",
    "    \n",
    "    if epoch%10 == 0:\n",
    "        print(epoch, 'loss:%.4f tracc:%.4f teacc:%.4f time:%.3f'%(\n",
    "            train_loss/len(train_data), train_acc/len(train_data), test_acc, time()-start)) \n",
    "    a.append(train_acc/len(train_data))\n",
    "    b.append(test_acc)\n",
    "\n",
    "print('tracc:%f teacc:%f'%(train_acc/len(train_data), test_acc))\n",
    "plt.plot(np.arange(epochs), a, np.arange(epochs), b)\n",
    "plt.ylim(0,1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
